package com.googlecode.recycled.xml;

import com.googlecode.recycled.lang.assertions.Assert;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * @author Henryk Konsek
 */
public class Elements {

    protected Elements() {
    }

    public static Element addElement(Node parent, String childName) {
        Assert.notNull(parent);
        Document document;
        if (parent instanceof Document) {
            if (parent.getChildNodes().getLength() > 0) {
                throw new IllegalArgumentException("For given document root element already exists.");
            }
            document = (Document) parent;
        } else {
            document = parent.getOwnerDocument();
        }
        Element childElement = document.createElement(Assert.notBlank(childName));
        parent.appendChild(childElement);
        return childElement;
    }

}
